package com.xps.and.yuntong.Data.net;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.xps.and.yuntong.App;
import com.jude.utils.JUtils;
import com.xps.and.yuntong.Ui.LoginActivity;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class LoggingInterceptor implements Interceptor {

    String str = "1";
    private String TAG = "LoggingInterceptor";
    @Override
    public Response intercept(@Nullable Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        SharedPreferences sp = JUtils.getSharedPreference();
        String access_token = sp.getString("access_token", "");
        if (!TextUtils.isEmpty(access_token)) {
            str = "1";
            builder.url(request.url().toString() + "?access-token=" + access_token);
        } else {
            builder.url(request.url().toString());
        }

        Response resp = chain.proceed(builder.build());
        Log.e("111",request.url().toString() + "?access-token=" + access_token);
        Log.e("111","111"+resp.code());
        if (!TextUtils.isEmpty(sp.getInt("user_id", 0)+"")) {
            if (resp.code() == 401) {
                if (str.equals("1")) {
                    Log.e("111","走了吗");
                    App.getInstance().exitApp();
                    str = "2";
                }
            }
        }
        return resp;
    }


}
